<?php

$projectId = @$_REQUEST["projectId"];
$userId = @$_REQUEST["userId"];
$description = @$_REQUEST["description"];
$mode = @$_REQUEST["mode"];

if ($projectId != null)
{
	include_once(__DIR__ . "/service/setting.php");
	
	global $dbHost;
	global $dbUser;
	global $dbPass;
	global $dbName;
	
	$db = @mysql_connect($dbHost, $dbUser, $dbPass);
	
	if ($db)
	{
		mysql_select_db($dbName, $db);
		
		$fileName = $_FILES["file"]["name"];
		$dt = date("Y-m-d H:i:s");
		
		if ($mode == "attachment")
		{
			$ticketId = @$_REQUEST["id"];
			
			$sql = "insert into cod_ticketattachment (ticketId, uploadTime, userId, fileName, description) " .
					"values ($ticketId, '$dt', '$userId', '" . mysql_real_escape_string($fileName) . "', '" . mysql_real_escape_string($description) . "')";
			
			mysql_query($sql, $db);
			
			$sql = "select max(id) as maxNo from cod_ticketattachment";
			
			$dbRet = mysql_query($sql, $db);
			
			if ($dbRet)
			{
				$dbRow = mysql_fetch_assoc($dbRet);
				
				if ($dbRow)
				{
					$max = $dbRow["maxNo"];
					move_uploaded_file($_FILES['file']['tmp_name'], __DIR__ . "/service/attachments/$max");
				}
			}
		}
		else
		{
			$sql = "insert into cod_file (projectId, uploadDate, uploadUserId, fileName, description) " .
					"values ($projectId, '$dt', '$userId', '" . mysql_real_escape_string($fileName) . "', '" . mysql_real_escape_string($description) . "')";
			
			mysql_query($sql, $db);
			
			$sql = "select max(id) as maxNo from cod_file";
			
			$dbRet = mysql_query($sql, $db);
			
			if ($dbRet)
			{
				$dbRow = mysql_fetch_assoc($dbRet);
				
				if ($dbRow)
				{
					$max = $dbRow["maxNo"];
					move_uploaded_file($_FILES['file']['tmp_name'], __DIR__ . "/service/files/$max");
				}
			}
		}
?>
<html>
	<head>
		<style type="text/css">
			html { height: 100%; overflow:hidden;  background: rgba(0, 0, 0, 0); }
			body { height: 100%; margin: 0px; padding: 10px; position:relative; background: rgba(0, 0, 0, 0); overflow:hidden; font-family : "Trebuchet MS", Arial, Helvetica, sans-serif; font-size: small;}
		</style>
		<script language="javascript">
			function getStatus()
			{
				return 1;
			}
		</script>
	</head>
	<body>
		OK
	</body>
</html>
<?php
	}
}
else
{
?>
<html>
	<head>
		<style type="text/css">
			html { height: 100%; overflow:hidden;  background: rgba(0, 0, 0, 0); }
			body { height: 100%; margin: 0px; padding: 10px; position:relative; background: rgba(0, 0, 0, 0); overflow:hidden; font-family : "Trebuchet MS", Arial, Helvetica, sans-serif; font-size: small;}
		</style>
		<script language="javascript">
			function send(projectId, userId, description, mode, id)
			{
				document.getElementById("info").style.display = "";
				document.getElementById("form").style.display = "none";
				
				document.getElementById("projectId").value = projectId;
				document.getElementById("userId").value = userId;
				document.getElementById("description").value = description;
				document.getElementById("mode").value = mode;
				document.getElementById("id").value = id;
				
				if ((document.getElementById("file").value != null) && (document.getElementById("file").value != ""))
					document.getElementById("form").submit();
				else
					alert("Please select a file first !");
			}
			
			function getStatus()
			{
				return 0;
			}
			
			function getFileName()
			{
				return document.getElementById("file").value;
			}
			
		</script>
	</head>
	<body>
		<form id="form" method="post" action="fileUpload.php" enctype="multipart/form-data">
			<input type="file" name="file" id="file" />
			<input type="hidden" name="projectId" id="projectId" value="-1" />
			<input type="hidden" name="userId" id="userId" value="-1" />
			<input type="hidden" name="description" id="description" value="-1" />
			<input type="hidden" name="mode" id="mode" value="-1" />
			<input type="hidden" name="id" id="id" value="-1" />
		</form>
		<div id="info" style="display:none">Uploading, please wait...</div>
	</body>
</html>
<?php
}
?>